package com.lyw.gulimall.coupon.service.impl;

import com.lyw.common.to.MemberPrice;
import com.lyw.common.to.SkuReductionTo;
import com.lyw.gulimall.coupon.entity.MemberPriceEntity;
import com.lyw.gulimall.coupon.entity.SkuLadderEntity;
import com.lyw.gulimall.coupon.service.MemberPriceService;
import com.lyw.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyw.common.utils.PageUtils;
import com.lyw.common.utils.Query;

import com.lyw.gulimall.coupon.dao.SkuFullReductionDao;
import com.lyw.gulimall.coupon.entity.SkuFullReductionEntity;
import com.lyw.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        // sku的优惠、满减信息 gulimall_sms -》 sms_sku_ladder/满减表 sms_sku_full_reduction/会员价格表 sms_member_price/商品积分表 sms_spu_bounds
        SkuLadderEntity ladderEntity = new SkuLadderEntity();
        ladderEntity.setSkuId(skuReductionTo.getSkuId());
        ladderEntity.setAddOther(skuReductionTo.getCountStatus());
        ladderEntity.setDiscount(skuReductionTo.getDiscount());
        ladderEntity.setFullCount(skuReductionTo.getFullCount());
        // 判断满几件是否有值
        if (skuReductionTo.getFullCount() > 0) {
            skuLadderService.save(ladderEntity);
        }

        // 2.满减信息
        SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo, fullReductionEntity);
        // 判断满减是否有值
        if (skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            this.save(fullReductionEntity);
        }

        // 3.会员价
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        if (memberPrice != null && memberPrice.size() > 0) {
            List<MemberPriceEntity> priceEntities = memberPrice.stream().map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                memberPriceEntity.setMemberPrice(item.getPrice());
                memberPriceEntity.setAddOther(1);

                return memberPriceEntity;
                // 过滤掉会员价格是 0 的
            }).filter(item -> item.getMemberPrice().compareTo(new BigDecimal("0")) == 1).collect(Collectors.toList());
            memberPriceService.saveBatch(priceEntities);
        }
    }
}